package com.dy.user.service.impl;

import com.dy.basic.service.impl.BaseServiceImpl;
import com.dy.basic.util.BusinessException;
import com.dy.basic.util.MD5Utils;
import com.dy.basic.util.StrUtils;
import com.dy.user.domain.Logininfo;
import com.dy.user.domain.User;
import com.dy.user.dto.EmailRegisterFormDto;
import com.dy.user.dto.PhoneRegisterFormDto;
import com.dy.user.mapper.LogininfoMapper;
import com.dy.user.mapper.UserMapper;
import com.dy.user.service.IUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dayong
 * @since 2023-03-04
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    public static final String PHONEREGISTER = "register:";

    /**
     * 手机注册用户的信息
     * @param phoneRegisterFormDto
     */
    @Override
    public void addByPhone(PhoneRegisterFormDto phoneRegisterFormDto) {
        // 两次密码是否一致
        if (!phoneRegisterFormDto.getPassword().equals(phoneRegisterFormDto.getPasswordRepeat())){
            throw new BusinessException("两次密码布不一致，请重新输入");
        }
        // 校验验证码是否过期

        // 判断手机验证码是否正确
        String code = (String) redisTemplate.opsForValue().get(String.format(PHONEREGISTER,phoneRegisterFormDto.getPhone()));
        if (StringUtils.isBlank(code) || !code.equals(phoneRegisterFormDto.getCode())){
            throw new BusinessException("手机验证码输入错误");
        }

        // 判断用户是否被注册
        User user1 = userMapper.findByPhone(phoneRegisterFormDto.getPhone());
        if (null != user1){
            throw new BusinessException("滚去登录 ");
        }

        User user = addUser(phoneRegisterFormDto);
        Logininfo logininfo = logininfo(user);
        logininfoMapper.add(logininfo);
        user.setLogininfoId(logininfo.getId());
        userMapper.add(user);
    }

    /**
     * 邮箱注册
     * @param emailRegisterFormDto
     */
    @Override
    public void addByEmail(EmailRegisterFormDto emailRegisterFormDto) {
        // 校验参数
        // 校验两次密码是否正确
        if (!emailRegisterFormDto.getPassword().equals(emailRegisterFormDto.getPasswordRepeat())){
            throw new BusinessException("两次密码输入不一致");
        }

        // 判断邮箱验证码是否输入正确
        String emailCodeByRedis = (String) redisTemplate.opsForValue().get(emailRegisterFormDto.getEmailCodeUUID());
        if (!emailCodeByRedis.equals(emailRegisterFormDto.getEmailCode())){
            throw new BusinessException("输入的邮箱验证码不正确!!");

        }
        // 判断邮箱是否被注册
        User userMapperByEmail = userMapper.findByEmail(emailRegisterFormDto.getEmail());
        if (null != userMapperByEmail){
            throw new BusinessException("该邮箱已被注册");
        }

        // 添加两张表的数据
        // 添加logininfo表
        Logininfo logininfo = new Logininfo();
        logininfo.setEmail(emailRegisterFormDto.getEmail());
        logininfo.setUsername(emailRegisterFormDto.getEmail());
        String salt1 = StrUtils.getComplexRandomString(32);
        logininfo.setSalt(salt1);
        String password = MD5Utils.encrypByMd5(salt1 + emailRegisterFormDto.getPassword());
        logininfo.setPassword(password);
        logininfoMapper.add(logininfo);

        // 添加user表
        User user = new User();
        BeanUtils.copyProperties(logininfo,user);
        user.setLogininfoId(logininfo.getId());
        userMapper.add(user);

    }

    /**
     * logininfo
     * @param user
     * @return
     */
    private Logininfo logininfo(User user) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user,logininfo);
        logininfo.setType(1);
        return logininfo;
    }

    /**
     * user
     * @param phoneRegisterFormDto
     * @return
     */
    private User addUser(PhoneRegisterFormDto phoneRegisterFormDto) {
        // 添加logininfo,user表数据
        User user = new User();
        user.setPhone(phoneRegisterFormDto.getPhone());
        user.setAge(18);
        user.setUsername(phoneRegisterFormDto.getPhone());
        String salt1 = StrUtils.getComplexRandomString(32);
        user.setSalt(salt1);
        String password = MD5Utils.encrypByMd5(salt1 + phoneRegisterFormDto.getPassword());
        user.setPassword(password);
        return user;
    }
}
